import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { getRooms, createRoom, updateRoom, deleteRoom, getRoomTypes, getRoomStatuses } from '@/api/room'
import { Message } from '@arco-design/web-vue'

export const useRoomStore = defineStore('room', () => {
  // 状态
  const rooms = ref([])
  const roomTypes = ref([])
  const roomStatuses = ref([])
  const loading = ref(false)
  const pagination = ref({
    current: 1,
    size: 10,
    total: 0
  })
  const filters = ref({
    search: '',
    type: '',
    status: ''
  })

  // 计算属性
  const roomTypeOptions = computed(() => 
    roomTypes.value.map(type => ({
      label: type.name,
      value: type.id
    }))
  )

  const roomStatusOptions = computed(() => 
    roomStatuses.value.map(status => ({
      label: status.name,
      value: status.id
    }))
  )

  // 获取房间列表
  const fetchRooms = async (params = {}) => {
    loading.value = true
    try {
      const data = await getRooms({
        page: pagination.value.current,
        size: pagination.value.size,
        ...filters.value,
        ...params
      })
      
      console.log('获取到房间数据', data)
      
      // 后端返回的是 list 而不是 rooms
      rooms.value = data.list || data.rooms || []
      pagination.value.total = data.total || 0
      pagination.value.current = data.page || data.current_page || 1
      
      console.log('房间列表已更新:', rooms.value.length, '条记录')
    } catch (error) {
      console.error('获取房间列表失败:', error)
      rooms.value = []
    } finally {
      loading.value = false
    }
  }

  // 获取房间类型
  const fetchRoomTypes = async () => {
    try {
      const data = await getRoomTypes()
      roomTypes.value = data
    } catch (error) {
      console.error('获取房间类型失败:', error)
    }
  }

  // 获取房间状态
  const fetchRoomStatuses = async () => {
    try {
      const data = await getRoomStatuses()
      roomStatuses.value = data
    } catch (error) {
      console.error('获取房间状态失败:', error)
    }
  }

  // 创建房间
  const createRoomData = async (roomData) => {
    try {
      await createRoom(roomData)
      Message.success('房间创建成功')
      await fetchRooms()
      return true
    } catch (error) {
      console.error('创建房间失败:', error)
      return false
    }
  }

  // 更新房间
  const updateRoomData = async (id, roomData) => {
    try {
      await updateRoom(id, roomData)
      Message.success('房间更新成功')
      await fetchRooms()
      return true
    } catch (error) {
      console.error('更新房间失败:', error)
      return false
    }
  }

  // 删除房间
  const deleteRoomData = async (id) => {
    try {
      await deleteRoom(id)
      Message.success('房间删除成功')
      await fetchRooms()
      return true
    } catch (error) {
      console.error('删除房间失败:', error)
      return false
    }
  }

  // 设置筛选条件
  const setFilters = (newFilters) => {
    filters.value = { ...filters.value, ...newFilters }
    pagination.value.current = 1
  }

  // 设置分页
  const setPagination = (newPagination) => {
    pagination.value = { ...pagination.value, ...newPagination }
  }

  // 重置筛选条件
  const resetFilters = () => {
    filters.value = {
      search: '',
      type: '',
      status: ''
    }
    pagination.value.current = 1
  }

  return {
    // 状态
    rooms,
    roomTypes,
    roomStatuses,
    loading,
    pagination,
    filters,
    
    // 计算属性
    roomTypeOptions,
    roomStatusOptions,
    
    // 方法
    fetchRooms,
    fetchRoomTypes,
    fetchRoomStatuses,
    createRoomData,
    updateRoomData,
    deleteRoomData,
    setFilters,
    setPagination,
    resetFilters
  }
})
